%%%----------------------------------------------------------------------
%%% File    : nc_operation.erl
%%% Author  : Sunny <sunjiangangok@gmail.com>
%%% Purpose : Process nc's message
%%% Created : 8 Apr 2011 by Sunny <sunjiangangok@gmail.com>
%%% Id      :
%%%----------------------------------------------------------------------

-module(nc_operation).
-author('sunjiangangok@gmail.com').

-behaviour(gen_fsm).

%% Include record files
-include("record.hrl").

%% External exports
-export([nc_init/1,
	operation/1]).

%% gen_fsm callbacks
-export([close/2,
	started/2,
	init/1,
	handle_event/3,
	handle_sync_event/4,
	code_change/4,
	handle_info/3,
	terminate/3]).

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
nc_init(NC_VM_START) ->
	gen_fsm:start_link({local, nc_operation}, nc_operation, NC_VM_START, []).
operation(Command) ->
	gen_fsm:send_event(nc_operation, {operation, Command}).

%%%----------------------------------------------------------------------
%%% Callback functions from gen_fsm
%%%----------------------------------------------------------------------

%%----------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, StateName, StateData}          |
%%          {ok, StateName, StateData, Timeout} |
%%          ignore                              |
%%          {stop, StopReason}                   
%%----------------------------------------------------------------------
init(NC_VM_START) ->
%	process_flag(trap_exit, true),
	{ok, close, {attribute, NC_VM_START}}.


%%----------------------------------------------------------------------
%% Func: StateName/2
%% Returns: {next_state, NextStateName, NextStateData}          |
%%          {next_state, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                         
%%----------------------------------------------------------------------
close({operation, Command}, {attribute, NC_VM_START}) ->
	case Command of
		start ->
			do_start({attribute, NC_VM_START}),
			{next_state, started, {attribute, NC_VM_START}, 2000};
		_Other ->
			io:format("The state is not changed.~n"),
			{next_state, close, {attribute, NC_VM_START}}
	end.
started(timeout, {attribute, NC_VM_START}) ->
	io:format("The vm has been started successfully...~n"),
	{stop, normal, {attribute, NC_VM_START}}.

%%----------------------------------------------------------------------
%% Func: StateName/3
%% Returns: {next_state, NextStateName, NextStateData}            |
%%          {next_state, NextStateName, NextStateData, Timeout}   |
%%          {reply, Reply, NextStateName, NextStateData}          |
%%          {reply, Reply, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                          |
%%          {stop, Reason, Reply, NewStateData}                    
%%----------------------------------------------------------------------
%state_name(Event, From, StateData) ->
%    Reply = ok,
%    {reply, Reply, state_name, StateData}.



%%----------------------------------------------------------------------
%% Func: handle_event/3
%% Returns: {next_state, NextStateName, NextStateData}          |
%%          {next_state, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                         
%%----------------------------------------------------------------------
handle_event(_Event, StateName, StateData) ->
    {next_state, StateName, StateData}.

%%----------------------------------------------------------------------
%% Func: handle_sync_event/4
%% Returns: {next_state, NextStateName, NextStateData}            |
%%          {next_state, NextStateName, NextStateData, Timeout}   |
%%          {reply, Reply, NextStateName, NextStateData}          |
%%          {reply, Reply, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                          |
%%          {stop, Reason, Reply, NewStateData}                    
%%----------------------------------------------------------------------
handle_sync_event(Event, From, StateName, StateData) ->
	{Event, From, StateName, StateData}.

code_change(_OldVsn, StateName, StateData, _Extra) ->
    {ok, StateName, StateData}.

%%----------------------------------------------------------------------
%% Func: handle_info/3
%% Returns: {next_state, NextStateName, NextStateData}          |
%%          {next_state, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                         
%%----------------------------------------------------------------------
handle_info(Info, StateName, StateData) ->
	{Info, StateName, StateData}.

%%----------------------------------------------------------------------
%% Func: terminate/3
%% Purpose: Shutdown the fsm
%% Returns: any
%%----------------------------------------------------------------------
terminate(_Reason, _StateName, _StateData) ->
	ok.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------
do_start({attribute, NC_VM_START}) ->
	#nc_vm_start{shell=ShellScript, num=Num} = NC_VM_START,
	io:format("The vm is starting...[~p]~n", [NC_VM_START]),
	case ShellScript of
		start ->
			Query = lists:concat(["start.sh ", Num]),
			_Port = open_port({spawn, Query}, [stream]);
		_Other ->
			io:format("ssssssssssssssssss~n")
	end,
	ok.
